package com.zyx.springmvcmsb.advanced.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @author zyx
 * @since 2021/10/5 18:29
 */
@Controller
public class UploadController {

    @RequestMapping("/upload")
    public String upload(@RequestParam("file") MultipartFile[] files,
                         @RequestParam(value = "desc", required = false) String desc) throws IOException {
        System.out.println("desc =====>>>>> " + desc);
        // 获取文件名称并进行上传
        for (MultipartFile file : files) {
            // 文件不为空则进行上传
            if (!file.isEmpty()) {
                String filename = file.getOriginalFilename();
                System.out.println("filename =====>>>>> " + filename);
                file.transferTo(new File("D:\\file\\" + filename));
            }
        }
        return "success";
    }
}
